﻿using System.Runtime.InteropServices.JavaScript;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace AuthZ.PolicyBased.Controllers;

/// <summary>
/// 帐号管理 - 模拟登录
/// </summary>
[ApiController]
[Route("api/login/")]
public class AccountController : ControllerBase
{
    // /// <summary>
    // /// 无Authorize约束
    // /// </summary>
    // [HttpGet("authorize/none")]
    // public string NonAuthorize()
    // {
    //     return "NonAuthorize";
    // }
    //
    // /// <summary>
    // /// 默认Authorize约束
    // /// </summary>
    // [Authorize]
    // [HttpGet("authorize/default")]
    // public string DefaultAuthorize()
    // {
    //     return "DefaultAuthorize";
    // }

    /// <summary>
    /// 登录
    /// </summary>
    [HttpPost("login")]
    public async Task<IActionResult> Login(int age=18)
    {
        if (age < 1 || age > 99)
        {
            return this.BadRequest("年龄参数错误");
        }
        var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
        identity.AddClaims(new[]
        {
            new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString("N")),
            new Claim(ClaimTypes.Name, $"Under{age}Age"),
            new Claim(ClaimTypes.DateOfBirth, DateTime.Now.AddYears(-age).ToString("yyyy-MM-dd"))
        });

        var principal = new ClaimsPrincipal(identity);
        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
        return Ok("登录成功");
    }

    
    // /// <summary>
    // /// 登录 - 年龄小于18岁
    // /// </summary>
    // [HttpPost("under18")]
    // public async Task<IActionResult> Under18Age()
    // {
    //     var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
    //     identity.AddClaims(new[]
    //     {
    //         new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString("N")),
    //         new Claim(ClaimTypes.Name, "Under18Age"),
    //         new Claim(ClaimTypes.DateOfBirth, DateTime.Now.AddYears(-16).ToString("yyyy-MM-dd"))
    //     });
    //
    //     var principal = new ClaimsPrincipal(identity);
    //     await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
    //     return Ok("登录成功");
    // }
    //
    // /// <summary>
    // /// 登录 - 年龄大于18岁
    // /// </summary>
    // [HttpPost("over18")]
    // public async Task<IActionResult> Over18Age()
    // {
    //     var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
    //     identity.AddClaims(new[]
    //     {
    //         new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString("N")),
    //         new Claim(ClaimTypes.Name, "Over18Age"),
    //         new Claim(ClaimTypes.DateOfBirth, DateTime.Now.AddYears(-20).ToString("yyyy-MM-dd"))
    //     });
    //
    //     var principal = new ClaimsPrincipal(identity);
    //     await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
    //     return Ok("登录成功");
    // }

    /// <summary>
    /// 小于18岁，Boos角色
    /// </summary>
    [HttpPost("under18/boss")]
    public async Task<IActionResult> Under18Boss()
    {
        var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
        identity.AddClaims(new[]
        {
            new Claim(ClaimTypes.NameIdentifier, Guid.NewGuid().ToString("N")),
            new Claim(ClaimTypes.Name, "Under18Boss"),
            new Claim(ClaimTypes.DateOfBirth, DateTime.Now.AddYears(-16).ToString("yyyy-MM-dd")),
            new Claim(ClaimTypes.Role, "InternetBarBoss")
        });

        var principal = new ClaimsPrincipal(identity);
        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
        return Ok("登录成功");
    }
}